package com.brayan.melinxa.gui.systemtray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import com.brayan.melinxa.logic.DirectoryMonitor;

public class Tray {
	DirectoryMonitor dirMon = null;
	
	public Tray(DirectoryMonitor dirMon) {
		this.dirMon = dirMon;
		TrayIcon trayIcon = null;
		
		if (SystemTray.isSupported()) {
			// get the SystemTray instance
			SystemTray tray = SystemTray.getSystemTray();

			ActionListener listenerIcon = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("icon action");
				}
			};
			
			ActionListener listenerOpen = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("open action");
				}
			};
			ActionListener listenerActivate = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("activate action");
				}
			};
			// Create a popup menu.
			PopupMenu popup = new PopupMenu();
			// Open the gui application.
			MenuItem openMenuItem = new MenuItem("Open Melinxa");
			openMenuItem.addActionListener(listenerOpen);
			popup.add(openMenuItem);
			popup.addSeparator();
			// Activate or deactivate the data shadowing.
			MenuItem activateMenuItem = new MenuItem("Activate Shadowing");
			activateMenuItem.addActionListener(listenerActivate);
			popup.add(activateMenuItem);
			
			// load an image
			//ImageIO img = new ImageIO.read(new File("media.mirror.png"));
			URL imgURL = ClassLoader.getSystemResource("media/Suivant-Sky-256.png");
			Image image = Toolkit.getDefaultToolkit().getImage(imgURL);
			// construct a TrayIcon
			trayIcon = new TrayIcon(image, "Melinxa - data shadowing not activated", popup);
			// set the TrayIcon properties
			trayIcon.addActionListener(listenerIcon);
			trayIcon.setImageAutoSize(true);

			try {
				tray.add(trayIcon);
			} catch (AWTException e) {
				System.err.println(e);
			}
			// ...
		} else {
			// disable tray option in your application or
			// perform other actions
			// ...
		}
	}
	// ...
	// some time later
	// the application state has changed - update the image
	// if (trayIcon != null) {
	// trayIcon.setImage(updatedImage);
	// }
	// ...

}
